package net.sxnic.workflow.utils;

import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WorkflowUtils {

	private static final Logger logger = LoggerFactory
			.getLogger(WorkflowUtils.class);
	
	private static Configuration wfconfig;

	public static String getWfConfig(String key){
		
		try {
			if(wfconfig ==null){
				wfconfig = new PropertiesConfiguration("workflow_zh_CN.properties");
			}
		} catch (ConfigurationException e) {
			logger.error("====read wfconfig error==="+e.toString());
		}

		return (String)wfconfig.getProperty(key);
	}
	
	/**
	 * 根据entity获取发文上传路径，最后带File.separator
	 * 
	 * @param entity
	 * @return
	 */
	public static String getSendDocPath(String id, String processName,
			String cyear) {
		String path = net.sxnic.comm.utils.PropertyUtil.getUploadPath();

		return path + processName + File.separator + cyear + File.separator
				+ id;
	}
	
	public static String md5(byte[] buffer) {
		StringBuffer md5Hash = new StringBuffer(32);
		try {
			byte[] b = java.security.MessageDigest.getInstance("MD5").digest(
					buffer);
			int len = b.length;
			for (int x = 0; x < len; x++) {
				md5Hash.append(String.format("%02x", b[x]));
			}
		} catch (java.security.NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return (md5Hash.toString());
	}

	public static String md5File(File file) {
		String md5Hash = "";
		try {
			FileInputStream in = new FileInputStream(file);
			int bytes = in.available();
			byte[] buffer = new byte[bytes];
			in.read(buffer);
			in.close();
			md5Hash = md5(buffer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return md5Hash;
	}

}
